//
//  HMOAuthViewController.swift
//  Weibo20
//
//  Created by HM on 16/9/20.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit

//  第三方登录视图控制器
//  新浪开发平台提供的APPKey
let WeiboAppKey = "2707543009"
//  授权回调页
let WeiboRedirect_Uri = "http://www.itcast.cn"

class HMOAuthViewController: UIViewController {
    //  MARK: -- 懒加载
    private lazy var webView: UIWebView = UIWebView()
    
    //  重写loadView, 目的把webView作为视图控制器的view
    override func loadView() {
        
        //  解决底部webView黑条
        //  改成透明解决黑条问题
        webView.isOpaque = false
        
        view = webView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        requestLogin()
        setupNavUI()
       
    }
    
    //  请求第三方登录
    private func requestLogin() {
        //  准备url地址
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(WeiboAppKey)&redirect_uri=\(WeiboRedirect_Uri)"
        
        //  创建urlrequest对象
        let urlRequest = URLRequest(url: URL(string: url)!)
        
        webView.loadRequest(urlRequest)
        
        
        
    }
    
    //  设置导航栏按钮
    private func setupNavUI() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target: self, action: #selector(cancelAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", target: self, action: #selector(autoFillAction))
        title = "微博"
    }
    
    //  MARK: -- 点击事件处理
    @objc private func cancelAction() {
    
        dismiss(animated: true, completion: nil)
    
    }
    
    @objc private func autoFillAction() {
    
        print("自动填充")
        
        
        webView.stringByEvaluatingJavaScript(from: "document.getElementById('userId').value = 'hao123guohaibin@163.com';document.getElementById('passwd').value = 'guohaibin123'")
        
        
        
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    

}
